package com.example.exercise.job;

import org.quartz.*;

public class ScheduleUtil {
    private ScheduleUtil(){}
    private static final String SCHEDULE_NAME = "demo1Key";
    /**
     * 触发器 KEY
     */
    public static TriggerKey getTriggerKey(String scheduleName){
        return TriggerKey.triggerKey(scheduleName) ;
    }
    /**
     * 定时器 Key
     */
    public static JobKey getJobKey(String scheduleName){
        return JobKey.jobKey(scheduleName) ;
    }
    /**
     * 表达式触发器
     */
    public static CronTrigger getCronTrigger(Scheduler scheduler, String scheduleName){
        try {
            return (CronTrigger)scheduler.getTrigger(getTriggerKey(scheduleName)) ;
        } catch (SchedulerException e){
            throw new RuntimeException("getCronTrigger Fail",e) ;
        }
    }
    /**
     * 创建定时器
     */
    public static void createJob(Scheduler scheduler, String scheduleName, Class<? extends Job> clazz) {
        try {
            // 构建定时器
            JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(getJobKey(scheduleName)).build() ;
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder
                    .cronSchedule("0/10 * * * * ?")
                    .withMisfireHandlingInstructionDoNothing() ;
            CronTrigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(getTriggerKey(scheduleName))
                    .withSchedule(scheduleBuilder).build() ;
            jobDetail.getJobDataMap().put("JOB_PARAM_KEY", SCHEDULE_NAME);
            scheduler.scheduleJob(jobDetail,trigger) ;
            // 如果该定时器处于暂停状态
//            pauseJob(scheduler) ;
        } catch (SchedulerException e){
            throw new RuntimeException("createJob Fail",e) ;
        }
    }
    /**
     * 更新定时任务
     */
    public static void updateJob(Scheduler scheduler, String scheduleName) {
        try {
            // 构建定时器
            TriggerKey triggerKey = getTriggerKey(scheduleName);
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?")
                    .withMisfireHandlingInstructionDoNothing();
            CronTrigger trigger = getCronTrigger(scheduler, scheduleName);
            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
            trigger.getJobDataMap().put("JOB_PARAM_KEY", SCHEDULE_NAME);
            scheduler.rescheduleJob(triggerKey, trigger);
            // 如果该定时器处于暂停状态
            // pauseJob(scheduler);
        } catch (SchedulerException e) {
            throw new RuntimeException("updateJob Fail",e) ;
        }
    }
    /**
     * 停止定时器
     */
    public static void pauseJob(Scheduler scheduler, String scheduleName){
        try {
            scheduler.pauseJob(getJobKey(scheduleName));
        } catch (SchedulerException e){
            throw new RuntimeException("pauseJob Fail",e) ;
        }
    }
    /**
     * 恢复定时器
     */
    public static void resumeJob(Scheduler scheduler, String scheduleName){
        try {
            scheduler.resumeJob(getJobKey(scheduleName));
        } catch (SchedulerException e){
            throw new RuntimeException("resumeJob Fail",e) ;
        }
    }
    /**
     * 删除定时器
     */
    public static void deleteJob (Scheduler scheduler, String scheduleName){
        try {
            scheduler.deleteJob(getJobKey(scheduleName));
        } catch (SchedulerException e){
            throw new RuntimeException("deleteJob Fail",e) ;
        }
    }
    /**
     * 执行定时器
     */
    public static void run(Scheduler scheduler, String scheduleName){
        try {
            JobDataMap dataMap = new JobDataMap() ;
            dataMap.put("JOB_PARAM_KEY", SCHEDULE_NAME);
            scheduler.triggerJob(getJobKey(scheduleName), dataMap);
        } catch (SchedulerException e){
            throw new RuntimeException("run Fail",e) ;
        }
    }

    public static void createOrUupdateJob(Scheduler scheduler, String scheduleName, Class<? extends Job> clazz) {
        CronTrigger cronTrigger1 = ScheduleUtil.getCronTrigger(scheduler, scheduleName) ;
        if (cronTrigger1 == null){
            ScheduleUtil.createJob(scheduler, scheduleName, clazz);
        } else {
            ScheduleUtil.updateJob(scheduler, scheduleName);
        }
    }
}
